package com.rt.easyjava.base.modules.timerjob.service.impl.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.easyjava.base.modules.timerjob.bean.TmptTimerBiz;
import com.rt.easyjava.base.modules.timerjob.dto.TmptTimerBizDto;
import com.rt.easyjava.base.modules.timerjob.mapper.TmptTimerBizMapper;
import com.rt.easyjava.base.modules.timerjob.service.intf.base.TmptTimerService;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 *  服务实现类
 * @author wuxiaoyong
 * @since 2020-06-01
 */
public class TmptTimerServiceImpl extends ServiceImpl<TmptTimerBizMapper, TmptTimerBiz> implements TmptTimerService {
    private final Logger logger = LoggerFactory.getLogger(TmptTimerServiceImpl.class);

    @Autowired
    protected MyMapper mapper;
    @Resource
    protected TmptTimerBizMapper tmptTimerBizMapper;

    // ==================================================================================
    // ========== 以下使用的是 Mybatis-plus 实现, 适合单表操作 ==============================
    // ==================================================================================

    /**
    * create a object
    * @param tmptTimerBizDto
    * @return
    */
    @Override
    public Result<TmptTimerBizDto> create(TmptTimerBizDto tmptTimerBizDto) {
        Result<TmptTimerBizDto> result = new Result<>();

        // dto to entity
        TmptTimerBiz tmptTimerBiz = mapper.map(tmptTimerBizDto, TmptTimerBiz.class);

        tmptTimerBizMapper.insert(tmptTimerBiz);

        return result.success();
    }

    /**
    * update a object by Id
    * @param tmptTimerBizDto
    * @param updateWrapper
    * @return
    */
    @Override
    public Result<TmptTimerBizDto> update(TmptTimerBizDto tmptTimerBizDto, UpdateWrapper updateWrapper) {
        Result<TmptTimerBizDto> result = new Result<>();

        // dto to entity
        TmptTimerBiz tmptTimerBiz = mapper.map(tmptTimerBizDto, TmptTimerBiz.class);

        tmptTimerBizMapper.update(tmptTimerBiz, updateWrapper);

        return result.success();
    }

    /**
    * 分页 (无合计功能)
    * @param page
    * @param queryWrapper
    * @return
    */
    public Result<TmptTimerBizDto> page(Page<TmptTimerBiz> page, QueryWrapper<TmptTimerBiz> queryWrapper){
        Result<TmptTimerBizDto> result = new Result<>();

        // mapper
        IPage<TmptTimerBiz> iPage = tmptTimerBizMapper.selectPage(page, queryWrapper);

        // 查询结果转换成DTO
        List<TmptTimerBizDto> tmptTimerBizDtoList= mapper.maplist(iPage.getRecords(), TmptTimerBizDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptTimerBizDtoList);

        return result.success();
    }

    /**
    * select all by queryWrapper
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptTimerBizDto> selectBy(QueryWrapper<TmptTimerBiz> queryWrapper) {
        Result<TmptTimerBizDto> result = new Result<>();

        List<TmptTimerBiz> list = tmptTimerBizMapper.selectList(queryWrapper);

        List<TmptTimerBizDto> dtoList = mapper.maplist(list, TmptTimerBizDto.class);

        return result.setList(dtoList).success();
    }

    /**
    * select one by id
    * @param id
    * @return
    */
    @Override
    public Result<TmptTimerBizDto> selectById(Long id) {
        Result<TmptTimerBizDto> result = new Result<>();

        TmptTimerBiz tmptTimerBiz = tmptTimerBizMapper.selectById(id);

        TmptTimerBizDto tmptTimerBizDto = mapper.map(tmptTimerBiz, TmptTimerBizDto.class);

        return result.setObj(tmptTimerBizDto).success();
    }




    // ==================================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现, 适合多表查询 =========================
    // ==================================================================================

    /**
    * 分页查询：mybatis plus 自定义sql分页查询
    * 默认开启合计查询，按需注掉代码
    * @param page
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptTimerBizDto> myPage(Page<TmptTimerBiz> page, QueryWrapper<TmptTimerBiz> queryWrapper) {
        Result<TmptTimerBizDto> result = new Result();

        IPage<TmptTimerBiz> iPage = tmptTimerBizMapper.myPage(page, queryWrapper);
        // 查询结果转换成DTO
        List<TmptTimerBizDto> tmptTimerBizDtoList = mapper.maplist(iPage.getRecords(), TmptTimerBizDto.class);
        // 返回结果集
        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptTimerBizDtoList);

        // 合计
        List<TmptTimerBiz> sumList = tmptTimerBizMapper.mySelectSum(queryWrapper);
        List<TmptTimerBizDto> sumDtoList = mapper.maplist(sumList, TmptTimerBizDto.class);
        result.getMap().put("footer", sumDtoList);

        return result.success();
    }

    @Override
    public Result<TmptTimerBizDto> mySelectBy(QueryWrapper<TmptTimerBiz> queryWrapper) {
        Result<TmptTimerBizDto> result = new Result();

        List<TmptTimerBiz> list = tmptTimerBizMapper.mySelectBy(queryWrapper);
        List<TmptTimerBizDto> dtoList = mapper.maplist(list, TmptTimerBizDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<TmptTimerBizDto> mySelectById(long id) {
        Result<TmptTimerBizDto> result = new Result();

        TmptTimerBiz tmptTimerBiz = tmptTimerBizMapper.mySelectById(id);
        TmptTimerBizDto dto = mapper.map(tmptTimerBiz,TmptTimerBizDto.class);
        result.setObj(dto);

        return result.success();
    }


}
